package com.hgyc.mom.wms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.WmsStockIn;
import com.hgyc.mom.wms.vo.WmsStockInParam;
import com.hgyc.mom.wms.service.WmsStockInService;
import com.hgyc.mom.wms.vo.WmsStockInVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 入库单Controller
 *
 * @author fwj
 * @since 2025-10-08
 */
@Tag(name = "入库单控制器", description = "入库单控制器")
@RestController
@RequestMapping("/wms/stockIn")
public class WmsStockInController {
    @Autowired
    private WmsStockInService wmsStockInService;

    @Operation(summary = "查询入库单列表", description = "查询入库单列表")
    @PostMapping("/pageList")
    public Result<IPage<WmsStockIn>> pageList(@Parameter(description = "查询参数") @RequestBody WmsStockInParam wmsStockInParam) {
        Page<WmsStockIn> page = new Page<>(wmsStockInParam.getPage(), wmsStockInParam.getPageSize());
        WmsStockIn wmsStockIn = new WmsStockIn();
        BeanUtils.copyProperties(wmsStockInParam, wmsStockIn);
        QueryWrapper<WmsStockIn> wrapper = QueryWrapperUtil.build(wmsStockIn);
        IPage<WmsStockIn> list = wmsStockInService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取入库单详细信息", description = "获取入库单详细信息")
    @GetMapping(value = "/{id}")
    public Result<WmsStockIn> getInfo(@Parameter(description = "入库单ID") @PathVariable("id") Long id) {
        return Result.success(wmsStockInService.getById(id));
    }
    @Operation(summary = "新增入库单", description = "新增入库单")
    @PostMapping
    public Result<?> save(@Parameter(description = "入库单") @RequestBody WmsStockInVO wmsStockInVO) {
        wmsStockInService.saveStockIn(wmsStockInVO);
        return Result.success();
    }

    @Operation(summary = "修改入库单", description = "修改入库单")
    @PutMapping
    public Result<?> update(@Parameter(description = "入库单") @RequestBody WmsStockInVO wmsStockInVO) {
        wmsStockInService.updateStockIn(wmsStockInVO);
        return Result.success();
    }

    @Operation(summary = "删除入库单", description = "删除入库单")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        wmsStockInService.removeStockIn(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsStockIn> util = new ExcelUtil<>(WmsStockIn.class);
        util.importTemplateExcel(response, "入库单");
    }

    @Operation(summary = "导出入库单列表", description = "导出入库单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockInParam wmsStockInParam) {
        WmsStockIn wmsStockIn = new WmsStockIn();
        BeanUtils.copyProperties(wmsStockInParam, wmsStockIn);
        QueryWrapper<WmsStockIn> wrapper = QueryWrapperUtil.build(wmsStockIn);
        List<WmsStockIn> list = wmsStockInService.list(wrapper);
        ExcelUtil<WmsStockIn> util = new ExcelUtil<>(WmsStockIn.class);
        util.exportExcel(response, list, "入库单数据");
    }

    @Operation(summary = "导入入库单列表", description = "导入入库单列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsStockIn> util = new ExcelUtil<>(WmsStockIn.class);
        List<WmsStockIn> wmsStockInList = util.importExcel(file.getInputStream());
        String message = wmsStockInService.importData(wmsStockInList, updateSupport);
        return Result.success(message);
    }
}
